﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the G.O.S.E(Game Online Server Engine) for .NET.
//
//                 2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;
#endregion

namespace Demo_G.O.S.E.ServerEngine.Map
{
    /// <summary>
    /// 
    /// </summary>
    internal class PooledEnumerable : IPooledEnumerable, IDisposable
    {
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// m_Enumerator是初始化构造时的实例
        /// </summary>
        private IPooledEnumerator m_Enumerator = null;
        #endregion

        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pooledEnumerator"></param>
        private PooledEnumerable( IPooledEnumerator pooledEnumerator )
        {
            m_Enumerator = pooledEnumerator;
        }
        #endregion

        #region zh-CHS 共有静态方法 | en Public Static Methods
        #region zh-CHS 私有静态成员变量 | en Private Static Member Variables
        /// <summary>
        /// 
        /// </summary>
        private static Queue<PooledEnumerable> s_InstancePool = new Queue<PooledEnumerable>();
        /// <summary>
        /// 
        /// </summary>
        private static object s_LockInstancePool = new object();
        /// <summary>
        /// 
        /// </summary>
        private static long s_Depth = 0;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pooledEnumerator"></param>
        /// <returns></returns>
        public static PooledEnumerable Instantiate( IPooledEnumerator enumerator )
        {
            if ( s_Depth >= 20 )
                Debug.WriteLine( "PooledEnumerable.Instantiate(...) - s_Depth >= 20 Warning: Make sure to call Free() on pooled enumerables!" );

            PooledEnumerable l_PooledEnumerable = null;

            Monitor.Enter( s_LockInstancePool );
            {
                if ( s_InstancePool.Count > 0 )
                {
                    l_PooledEnumerable = s_InstancePool.Dequeue();
                    l_PooledEnumerable.m_Enumerator = enumerator;
                }
            }
            Monitor.Exit( s_LockInstancePool );

            if ( l_PooledEnumerable == null )
                l_PooledEnumerable = new PooledEnumerable( enumerator );

            enumerator.Enumerable = l_PooledEnumerable;

            Interlocked.Increment( ref s_Depth );

            return l_PooledEnumerable;
        }
        #endregion

        #region zh-CHS 接口实现 | en Interface Implementation
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator GetEnumerator()
        {
            if ( m_Enumerator == null )
                Debug.WriteLine( "PooledEnumerable.GetEnumerator(...) - m_Enumerator == null error:GetEnumerator(...) called after Free(...)!" );

            return m_Enumerator;
        }

        /// <summary>
        /// 释放自己(数据不再有效)
        /// </summary>
        public void Release()
        {
            if ( m_Enumerator != null )
            {
                m_Enumerator.Release();
                m_Enumerator = null;

                Monitor.Enter( s_LockInstancePool );
                {
                    s_InstancePool.Enqueue( this );
                }
                Monitor.Exit( s_LockInstancePool );

                Interlocked.Decrement( ref s_Depth );
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            Release();
        }
        #endregion
    }
}
#endregion

